﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StyleMVVM.Data.Activation;
using StyleMVVM.DependencyInjection;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;

namespace StyleMVVM.Notification.Badge
{
	public sealed class BadgeService : IBadgeService, IActivationAware
	{
		private BadgeUpdater updater;

		public static void RegisterExport(IDependencyInjectionContainer container)
		{
			container.Register(typeof(BadgeService)).As(typeof(IBadgeService)).AndSharedPermenantly();
		}

		public void UdpateGlyph(BadgeGlyph glyph)
		{
			string updateString = string.Format(BadgeUpdateString, glyph.ToString().ToLower());

			XmlDocument document = new XmlDocument();
			document.LoadXml(updateString);

			BadgeNotification notification = new BadgeNotification(document);

			updater.Update(notification);
		}

		public void UpdateNumber(int number)
		{
			string updateString = string.Format(BadgeUpdateString, number);

			XmlDocument document = new XmlDocument();
			document.LoadXml(updateString);

			BadgeNotification notification = new BadgeNotification(document);

			updater.Update(notification);
		}

		public void Activate()
		{
			updater = BadgeUpdateManager.CreateBadgeUpdaterForApplication();
		}

		private const string BadgeUpdateString = "<badge value=\"{0}\" />";
	}
}